package demo5.junit.mock;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DefaultDao implements IDao {

	@Override
	public boolean exist(User user) {
		boolean st = false;
		Connection con = null;
		PreparedStatement ps  = null;
		ResultSet rs  = null;
		try {

			Class.forName("org.sqlite.JDBC");

			// creating connection with the database
			con = DriverManager.getConnection(
					"jdbc:sqlite:/d:/work/eclipse-luna/workspace20160305/demo-jspservlet/j2ee.db");
			ps = con
					.prepareStatement("select * from users where username=? and pass=?");
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPassword());
			rs = ps.executeQuery();
			st = rs.next();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs!=null)
					rs.close();
				if(ps!=null)
					ps.close();
				if(con!=null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return st;
	}

	
}
